// 用在extframe框架中,代替原iframe模式,实现真正意义上的ext grid application
document
		.write("<script src=\""
				+ context
				+ "/temp-store/jsstuff/tableview/bestboy.js\" type=\"text/javascript\"></script>");

/**
 * 初始化视图(作为tabpanel的tab)
 * 
 * @param {}
 *            options
 */
function initTableViewTab(options) {
	Ext.MessageBox.wait('视图初始化过程中...');

	var url = context + "/temp-store/jsstuff/tableview/"
			+ tableviewStuff[options.id] + "/" + options.id + ".json";

	Ext.Ajax.request({
				url : url,
				params : {},
				method : 'POST',
				success : function(rs, request) {
					var result = rs.responseText;// 拿到结果集，此时为字符串

					var resultJson = Ext.util.JSON.decode(result);// 将字符串转换为json类型

					if (resultJson.success) {
						var viewSize = Ext.util.JSON.decode(resultJson.size);
						resultJson.size = viewSize;

						createTableViewTab(resultJson, options);
					}
				}
			});
}

/**
 * 从json结构定义中初始化form
 * 
 * @param {}
 *            resultJson
 * @param {}
 *            options
 */
function createTableViewTab(resultJson, options) {
	if (Ext.getCmp(options.tabid) == null) {
		// 默认参数
		var params = options.params;

		// 存在主表主键value,执行编辑操作
		if (params[resultJson.primaryTableColumnFormName]) {
			var selectDataJson = {
				id : params[resultJson.primaryTableColumnFormName],
				primaryTable : resultJson.primaryTable
			}

			if (resultJson.componentInList != null) {
				var foreignTables = [];

				Ext.each(resultJson.componentInList, function(n, i) {
							var json = {
								foreignTable : n.forTable,
								listid : n.id
							};

							if (n.orderby) {
								json.orderby = n.orderby;
							}

							if (n.sort) {
								json.sort = n.sort;
							}

							foreignTables.push(json);
						});

				selectDataJson.foreignTables = foreignTables;
			}

			initTableII(jsonToString(selectDataJson), function(o) {
				options.beanData = o.resultData;

				var form = getTableViewForm(resultJson, options, {
							id : options.tabid,
							title : options.title,
							autoScroll : true,
							iconCls : 'tabs',
							border : false,
							frame : true,
							closable : true,
							buttons : [{
								text : '提 交',
								handler : function() {
									methods.submitViewForm(resultJson, form,
											function() {
												Ext.Msg.alert('数据提交处理成功');
												Ext
														.getCmp(options.tabcontainerid)
														.remove(form);
											});
								}
							}, {
								text : '关 闭',
								handler : function() {
									Ext.getCmp(options.tabcontainerid)
											.remove(form);
								}
							}]
						});

				Ext.MessageBox.hide();

				Ext.getCmp(options.tabcontainerid).add(form).show();
			})
		} else {
			var form = getTableViewForm(resultJson, options, {
						id : options.tabid,
						title : options.title,
						autoScroll : true,
						iconCls : 'tabs',
						border : false,
						frame : true,
						closable : true,
						buttons : [{
							text : '提 交',
							handler : function() {
								methods.submitViewForm(resultJson, form,
										function() {
											Ext.Msg.alert('数据提交处理成功');
											Ext.getCmp(options.tabcontainerid)
													.remove(form);
										});
							}
						}, {
							text : '关 闭',
							handler : function() {
								Ext.getCmp(options.tabcontainerid).remove(form);
							}
						}]
					});

			Ext.MessageBox.hide();

			Ext.getCmp(options.tabcontainerid).add(form).show();
		}

	} else {
		Ext.getCmp(options.tabcontainerid).setActiveTab(Ext
				.getCmp(options.tabid));
	}
	// Ext.getCmp(options.tabcontainerid).setActiveTab(form);
}

function createTableViewWindow(resultJson, options) {

	var width = 500;
	var height = 400;

	var viewSize = Ext.util.JSON.decode(resultJson.size);
	resultJson.size = viewSize;

	try {
		width = ((viewSize.width == null || isNaN(viewSize.width))
				? 500
				: viewSize.width);
		height = ((viewSize.height == null || isNaN(viewSize.height))
				? 400
				: viewSize.height);
	} catch (error) {
		alert('视图尺寸设置有误,尺寸设置格式必须为json,形如{width:500, height:400}');
		return;
	}
	// 默认参数
	var params = options.params;

	// 存在主表主键value,执行编辑操作
	if (params[resultJson.primaryTableColumnFormName]) {
		var selectDataJson = {
			id : params[resultJson.primaryTableColumnFormName],
			primaryTable : resultJson.primaryTable
		}

		if (resultJson.componentInList != null) {
			var foreignTables = [];

			Ext.each(resultJson.componentInList, function(n, i) {
						var json = {
							foreignTable : n.forTable,
							listid : n.id
						};

						if (n.orderby) {
							json.orderby = n.orderby;
						}

						if (n.sort) {
							json.sort = n.sort;
						}

						foreignTables.push(json);
					});

			selectDataJson.foreignTables = foreignTables;
		}

		initTableII(jsonToString(selectDataJson), function(o) {
					options.beanData = o.resultData;

					var form = getTableViewForm(resultJson, options, {
								bodyStyle : 'padding:5px;',
								autoScroll : true,
								border : false,
								frame : true
							});

					var viewWin = new Ext.Window({
								renderTo : Ext.getBody(),
								layout : 'fit',
								width : width,
								height : height,
								title : resultJson.name,
								plain : true,
								modal : true,
								border : false,

								items : [form],

								buttons : [{
									text : '提 交',
									handler : function() {
										methods.submitViewForm(resultJson,
												form, function() {
													Ext.Msg.alert('数据提交处理成功');
													if (options.gridId) {
														reloadTQList(options.gridId);
													}

													viewWin.close();
													viewWin = null;
												});
									}
								}, {
									text : '关 闭',
									handler : function() {
										viewWin.close();
										viewWin = null;
									}
								}]
							});

					viewWin.show(this);
				});
	} else {
		var form = getTableViewForm(resultJson, options, {
					bodyStyle : 'padding:5px;',
					autoScroll : true,
					border : false,
					frame : true
				});

		var viewWin = new Ext.Window({
					renderTo : Ext.getBody(),
					layout : 'fit',
					width : width,
					height : height,
					title : resultJson.name,
					plain : true,
					modal : true,
					border : false,

					items : [form],

					buttons : [{
						text : '提 交',
						handler : function() {
							methods.submitViewForm(resultJson, form,
									function() {
										Ext.Msg.alert('数据提交处理成功');
										if (options.gridId) {
											reloadTQList(options.gridId);
										}

										viewWin.close();
										viewWin = null;
									});
						}
					}, {
						text : '关 闭',
						handler : function() {
							viewWin.close();
							viewWin = null;
						}
					}]
				});

		viewWin.show(this);
	}

}

var tableViewFormIdx = 0;

/**
 * 视图form
 * 
 * @param {}
 *            view resultJson
 * @param {}
 *            options
 * @param {}
 *            formObj
 * @return {}
 */
function getTableViewForm(resultJson, options, formObj) {
	var frozen = resultJson.frozen != null ? resultJson.frozen : false;

	var formId = resultJson.id + '_viewform_';

	if (!frozen) {
		formId += tableViewFormIdx;
		tableViewFormIdx++;
	}

	var urlparam = options.params;

	var componentItems = resultJson.component;

	// 未分组item
	var items = [];

	// 隐藏域item
	var hiddenItems = [];;

	// 密码item
	var pwdItems;

	// 是否上传文件附件
	var fileUpload = false;

	// 全部分组item
	var allGroupItems;

	// checkbox items
	var checkboxes;

	// radio items
	var radios;

	var groupObjectInList = false;

	Ext.each(componentItems, function(item) {
		if (options.beanData != null) {
			methods.initBeanValue(item, options.beanData);
		}

		var o = item.param;

		// 分组
		if (o.group != null) {
			groupObjectInList = true;

			if (allGroupItems == null) {
				allGroupItems = [];
			}

			allGroupItems.push({
						name : o.group,
						width : (o.groupWidth == null ? 800 : o.groupWidth),
						columnCount : o.columnCount,
						hookList : o.hookList,
						items : []
					});

			groupObj = null;
		}

		if (item.type == 'input') {

			/*
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('input类型的视图组件<' +
			 * item.name + '>的参数格式必须为json,形如{allowBlank:false, readOnly:true}');
			 * return; }
			 */

			var it = {
				fieldLabel : item.label,
				name : item.formName,
				xtype : 'textfield',
				allowBlank : (o.allowBlank == null || o.allowBlank),
				readOnly : o.readOnly,
				value : methods.getDefaultValue(item.defaultValue, urlparam),
				anchor : '90%'
			}

			if (o.vtype != null) {
				it.vtype = o.vtype;
			}

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}
		} else if (item.type == 'number') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('number类型的视图组件<' +
			 * item.name +
			 * '>的参数格式必须为json,形如{allowBlank:false,allowDecimals:true,allowNegative:false}'); }
			 */

			var it = {
				fieldLabel : item.label,
				name : item.formName,
				xtype : 'numberfield',
				allowBlank : (o.allowBlank == null || o.allowBlank),
				readOnly : o.readOnly,
				allowDecimals : (o.allowDecimals != null && o.allowDecimals), // 允许小数点
				allowNegative : (o.allowNegative != null && o.allowNegative), // 允许负数
				value : methods.getDefaultValue(item.defaultValue, urlparam),
				anchor : '90%'
			};

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}
		} else if (item.type == 'money') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('number类型的视图组件<' +
			 * item.name +
			 * '>的参数格式必须为json,形如{allowBlank:false,allowDecimals:true,allowNegative:false}'); }
			 */

			var dv = methods.getDefaultValue(item.defaultValue, urlparam);

			var it = new Ext.form.TextField({
				fieldLabel : item.label,
				name : item.formName + '_',
				allowBlank : (o.allowBlank == null || o.allowBlank),
				readOnly : o.readOnly,
				value : methods.RMBMoney(dv),
				listeners : {
					'blur' : function() {
						if (trim(it.getValue()) != '') {
							if (!isNaN(it.getValue())) {
								viewForm.getForm().findField(item.formName)
										.setValue(it.getValue());
								it.setValue(methods.RMBMoney(it.getValue()));
							} else {
								Ext.Msg.alert('提示', '请输入的数字', function(btn) {
											it
													.setValue(methods
															.RMBMoney(viewForm
																	.getForm()
																	.findField(item.formName)
																	.getValue()));
										});

							}
						}
					},
					'focus' : function() {
						if (viewForm.getForm().findField(item.formName)
								.getValue() != '') {
							it.setValue(viewForm.getForm()
									.findField(item.formName).getValue());
						}
					}
				},
				anchor : '90%'
			});
			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}

			var it1 = {
				xtype : 'hidden',
				name : item.formName,
				value : dv
			};
			hiddenItems.push(it1);
		} else if (item.type == 'htmleditor') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('htmleditor类型的视图组件<' +
			 * item.name + '>的参数格式必须为json,形如{enableAlignments:false,
			 * readOnly:true}'); }
			 */

			var it = new Ext.form.HtmlEditor({
						fieldLabel : item.label,
						enableAlignments : (o.enableAlignments == true),
						enableColors : (o.enableColors == true),
						enableFont : (o.enableFont == true),
						enableFontSize : (o.enableFontSize == true),
						enableLinks : (o.enableLinks == true),
						enableFormat : (o.enableFormat == true),
						enableLists : (o.enableLists == true),
						enableSourceEdit : (o.enableSourceEdit == true),
						name : item.formName,
						allowBlank : (o.allowBlank == null || o.allowBlank),
						readOnly : o.readOnly,
						value : item.defaultValue,
						anchor : '90%'
					});
			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}
		} else if (item.type == 'file') {

			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('file类型的视图组件<' + item.name +
			 * '>的参数格式必须为json,形如{fileType:\'\'}'); }
			 */
			if (fileUpload != true) {
				fileUpload = true;
			}

			if (item.defaultValue != '') {
				Ext.each(item.defaultValue, function(file) {
					//item.type
							var it = {
								xtype : 'box',
								name : item.formName,
								fieldLabel : item.label,
								html : '<a href="javascript:viewAnnex(\''
										+ file.id + '\');">' + file.name + '</a>'
							}

							if (groupObjectInList) {
								allGroupItems[allGroupItems.length - 1].items
										.push(it);
							} else {
								items.push(it);
							}
						});
			} else {
				var it = {
					xtype : 'fileuploadfield',
					name : item.formName,
					fieldLabel : item.label,
					buttonText : '',
					buttonCfg : {
						iconCls : 'upload-icon'
					},
					anchor : '90%'
				};

				if (groupObjectInList) {
					allGroupItems[allGroupItems.length - 1].items.push(it);
				} else {
					items.push(it);
				}
			}

		} else if (item.type == 'textarea') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('textarea类型的视图组件<' +
			 * item.name + '>的参数格式必须为json,形如{allowBlank:false, readOnly:true}'); }
			 */

			var it = new Ext.form.TextArea({
						fieldLabel : item.label,
						name : item.formName,
						allowBlank : (o.allowBlank == null || o.allowBlank),
						readOnly : o.readOnly,
						value : methods.getDefaultValue(item.defaultValue,
								urlparam),
						anchor : '90%'
					});
			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}
		} else if (item.type == 'password') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('password类型的视图组件<' +
			 * item.name + '>的参数格式必须为json,形如{allowBlank:false, readOnly:true,
			 * confirmTo:\'\'}'); }
			 * 
			 * if (o.confirmTo != null && Ext.getCmp(o.confirmTo) == null) {
			 * alert('密码确认对象为空,将无法完成密码确认校验'); }
			 */

			var it = new Ext.form.TextField({
						fieldLabel : item.label,
						name : item.formName,
						inputType : 'password',
						allowBlank : (o.allowBlank == null || o.allowBlank),
						readOnly : o.readOnly,
						value : methods.getDefaultValue(item.defaultValue,
								urlparam),
						anchor : '90%'
					});

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}

			if (o.confirmTo) {

				if (pwdItems == null) {
					pwdItems = [];
				}

				pwdItems.push(it);

				var confirmToObj = new Ext.form.TextField({
							fieldLabel : item.label + '确认',
							name : item.formName + '_',
							inputType : 'password',
							allowBlank : false,
							vtype : 'password',
							confirmTo : {
								formId : formId,
								confirmTo : item.formName
							},
							value : '',
							anchor : '90%'
						});

				if (groupObjectInList) {
					allGroupItems[allGroupItems.length - 1].items
							.push(confirmToObj);
				} else {
					items.push(confirmToObj);
				}
			}

		} else if (item.type == 'hidden') {
			var it = {
				xtype : 'hidden',
				name : item.formName,
				value : methods.getDefaultValue(item.defaultValue, urlparam)
			};

			hiddenItems.push(it);
		} else if (item.type == 'date' || item.type == 'time') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('date类型的视图组件<' + item.name +
			 * '>的参数格式必须为json,形如{allowBlank:false}'); }
			 */
			var dv = methods.getDefaultValue(item.defaultValue, urlparam);

			var dvString = '';

			if (dv != '') {
				if (item.type == 'date') {
					dvString = formatDate(dv);
				} else if (item.type == 'time') {
					dvString = formatTime(dv);
				}
			}

			var it = {
				xtype : 'datefield',
				fieldLabel : item.label,
				name : item.formName + '_assistant_',
				format : 'Y-m-d',
				value : dvString,
				allowBlank : (o.allowBlank == null || o.allowBlank),
				listeners : {
					select : function(m, d) {
						viewForm.getForm().findField(item.formName)
								.setValue(parseDate(m.value) + '000000');
					},
					blur : function(m, d) {
						if (m != '') {
							viewForm.getForm().findField(item.formName)
									.setValue(parseDate(m.value) + '000000');
						}
					}
				},
				anchor : '90%'
			};

			if (o.dateRange != null) {
				o.dateRange.formId = formId;
				it.dateRange = o.dateRange;
				it.vtype = 'dateRange';
			}

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}

			var it1 = {
				xtype : 'hidden',
				name : item.formName,
				value : dv
			};
			hiddenItems.push(it1);

			// special for queryForm data between startDate and endDate
			if (item.mode != null && item.mode == '09') {
				var dv = methods.getDefaultValue(item.defaultValue, urlparam);

				var dvString = '';

				if (dv != '') {
					if (item.type == 'date') {
						dvString = formatDate(dv);
					} else if (item.type == 'time') {
						dvString = formatTime(dv);
					}
				}

				var it = {
					xtype : 'datefield',
					fieldLabel : '至',
					name : item.formName + '_betweenand_assistant_',
					format : 'Y-m-d',
					value : dvString,
					allowBlank : (o.allowBlank == null || o.allowBlank),
					listeners : {
						select : function(m, d) {
							viewForm.getForm().findField(item.formName + '_')
									.setValue(parseDate(m.value) + '000000');
						},
						blur : function(m, d) {
							if (m != '') {
								viewForm.getForm().findField(item.formName
										+ '_').setValue(parseDate(m.value)
										+ '000000');
							}
						}
					},
					anchor : '90%'
				};

				if (groupObjectInList) {
					allGroupItems[allGroupItems.length - 1].items.push(it);
				} else {
					items.push(it);
				}

				var it2 = {
					xtype : 'hidden',
					name : item.formName + '_',
					value : dv
				};
				hiddenItems.push(it2);
			}
		} else if (item.type == 'radio') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('radio类型的视图组件<' +
			 * item.name +
			 * '>的参数格式必须为json,形如{items:[{label:\'工作日\',value:\'0\'},{label:\'非工作日\',value:\'-1\'}]}');
			 * return; }
			 */
			var dv = methods.getDefaultValue(item.defaultValue, urlparam);

			if (dv == '' && item.param.checkItem != null) {
				dv = item.param.checkItem;
			}

			var radioGroup = new Ext.form.RadioGroup({
						fieldLabel : item.label,
						// columns : o.columns == null ? 1 : o.columns,
						allowBlank : (o.allowBlank == null || o.allowBlank),
						items : []
					});

			var radioIndex = 0;

			var targetItem;

			Ext.each(o.items, function(radioItem) {
						var it = new Ext.form.Radio({ // 以上相同
							boxLabel : radioItem.name,
							name : item.formName + '_',
							inputValue : radioItem.id,
							checked : (dv == radioItem.id),
							listeners : {
								check : function(checkitem, checked) {
									if (checked) {
										viewForm.getForm()
												.findField(item.formName)
												.setValue(radioItem.id);

										if (targetItem != null) {
											viewForm
													.getForm()
													.findField(targetItem.formName)
													.setValue(radioItem.name);
										}
									}
								}
							}
						});

						radioGroup.items.push(it);

						radioIndex++;
					});

			hiddenItems.push({
						xtype : 'hidden',
						name : item.formName,
						value : dv
					});

			if (o.target != null) {
				targetItem = methods.findComponentItems(componentItems,
						o.target);

				if (targetItem == null) {
					alert('radio类型的视图组件<' + item.name + '>的目标组件<id=' + o.target
							+ '>并不存在,会影响数据的完整性');
				}
			}

			if (radios == null) {
				radios = [];
			}

			radios.push({
						radioGroup : radioGroup,
						idsItem : item.formName,
						namesItem : o.target,
						size : radioIndex
					});

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(radioGroup);
			} else {
				items.push(radioGroup);
			}
		} else if (item.type == 'checkbox') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('radio类型的视图组件<' +
			 * item.name +
			 * '>的参数格式必须为json,形如{items:[{label:\'工作日\',value:\'0\'},{label:\'非工作日\',value:\'-1\'}]}');
			 * return; }
			 */

			var dv = methods.getDefaultValue(item.defaultValue, urlparam);

			if (dv == '' && item.param.checkItem != null) {
				dv = item.param.checkItem;
			}

			var checkboxItems = [];

			var checkboxIndex = 0;

			Ext.each(o.items, function(checkboxItem) {
						var it = new Ext.form.Checkbox({ // 以上相同
							boxLabel : checkboxItem.name,
							name : item.formName + '_' + checkboxIndex,
							inputValue : checkboxItem.id,
							checked : methods.checkItemInGroup(dv,
									checkboxItem.id),
							listeners : {
								check : function(checkbox, checked) {
									if (checked) {
										// alert(checkbox.inputValue);

										// item.param.forItem
									}
								}
							}
						});

						checkboxItems.push(it);

						checkboxIndex++;
					});

			var checkboxGroup = new Ext.form.CheckboxGroup({
						fieldLabel : item.label,
						columns : o.columns == null ? 1 : o.columns,
						allowBlank : (o.allowBlank == null || o.allowBlank),
						items : checkboxItems
					});

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items
						.push(checkboxGroup);
			} else {
				items.push(checkboxGroup);
			}

			var it1 = {
				xtype : 'hidden',
				name : item.formName,
				value : dv
			};
			hiddenItems.push(it1);

			var targetItem;
			if (o.target != null) {
				targetItem = methods.findComponentItems(componentItems,
						o.target);

				if (targetItem == null) {
					alert('checkbox类型的视图组件<' + item.name + '>的目标组件<id='
							+ o.target + '>并不存在,会影响数据的完整性');
				}
			}

			if (checkboxes == null) {
				checkboxes = [];
			}

			checkboxes.push({
						checkboxGroup : checkboxGroup,
						idsItem : item.formName,
						namesItem : o.target,
						size : checkboxIndex
					});
		} else if (item.type == 'select') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON.decode(item.param); } catch (error) {
			 * alert('select类型的视图组件<' + item.name +
			 * '>的参数格式必须为json,形如{type:\'rating\',target:\'userId\'}'); }
			 */
			var targetItem;

			if (o.target == null) {
				alert('select类型的视图组件<' + item.name + '>的目标组件未定义,会影响数据的完整性');
				targetItem = {
					formName : item.formName + '_notarget'
				}
			} else {
				targetItem = methods.findComponentItems(componentItems,
						o.target);

				if (targetItem == null) {
					alert('select类型的视图组件<' + item.name + '>的目标组件<id='
							+ o.target + '>并不存在,会影响数据的完整性');
					targetItem = {
						formName : item.formName + '_notarget'
					}
				} else {
					methods.initBeanValue(targetItem, options.beanData);
				}
			}

			var it = getWBComboStore(o.wordbook, item.label, item.formName,
					targetItem.formName,
					(o.allowBlank == null || o.allowBlank), formId);

			if (trim(targetItem.defaultValue) != '') {
				it.setValue(targetItem.defaultValue);
			}

			it.anchor = '90%';

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(it);
			} else {
				items.push(it);
			}

			hiddenItems.push({
						xtype : 'hidden',
						name : item.formName,
						value : methods.getDefaultValue(item.defaultValue,
								urlparam)
					});
		} else if (item.type == 'guselect' || item.type == 'gselect'
				|| item.type == 'uselect' || item.type == 'guselect1'
				|| item.type == 'gselect1' || item.type == 'uselect1'
				|| item.type == 'roleselect' || item.type == 'roleselect1') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON.decode(item.param); } catch (error) {
			 * alert('guselect或者roleselect类型的视图组件<' + item.name +
			 * '>的参数格式必须为json,形如{root:\'0001\',targetName:\'userName\',targetType:\'userType\'}'); }
			 */
			var targetNameItem;

			if (o.targetName == null) {
				alert('guselect或者roleselect类型的视图组件<' + item.name
						+ '>的目标名称组件并未定义,会影响数据的完整性');
				targetNameItem = {
					formName : item.formName + '_nonametarget'
				}
			} else {
				targetNameItem = methods.findComponentItems(componentItems,
						o.targetName);

				if (targetNameItem == null) {
					alert('guselect或者roleselect类型的视图组件<' + item.name
							+ '>的目标组件<id=' + o.targetName + '>并不存在,会影响数据的完整性');
					targetNameItem = {
						formName : item.formName + '_nonametarget'
					}
				} else {
					methods.initBeanValue(targetNameItem, options.beanData);
				}
			}

			var targetTypeItem;

			if (o.targetType == null) {
				alert('guselect或者roleselect类型的视图组件<' + item.name
						+ '>的目标类型组并未定义,会影响数据的完整性');
				targetTypeItem = {
					formName : item.formName + '_notypetarget'
				}
			} else {
				targetTypeItem = methods.findComponentItems(componentItems,
						o.targetType);
				if (targetTypeItem == null) {
					alert('guselect或者roleselect类型的视图组件<' + item.name
							+ '>的目标组件<id=' + o.targetType + '>并不存在,会影响数据的完整性');
					targetTypeItem = {
						formName : item.formName + '_notypetarget'
					}
				}
			}

			var gucombox;

			if (item.type == 'guselect') {
				gucombox = getGroupUserChkCombo4Single(item.label,
						item.formName, targetNameItem.formName,
						targetTypeItem.formName, o.root,
						(o.allowBlank == null || o.allowBlank), formId);
			} else if (item.type == 'gselect') {
				gucombox = getGroupSelectCombo4Single(item.label,
						item.formName, targetNameItem.formName,
						targetTypeItem.formName, o.root,
						(o.allowBlank == null || o.allowBlank), formId);
			} else if (item.type == 'uselect') {
				gucombox = getUserChkCombo4Single(item.label, item.formName,
						targetNameItem.formName, targetTypeItem.formName,
						o.root, (o.allowBlank == null || o.allowBlank), formId);
			} else if (item.type == 'guselect1') {
				gucombox = getGroupUserChkCombo(item.label, item.formName,
						targetNameItem.formName, targetTypeItem.formName,
						o.root, (o.allowBlank == null || o.allowBlank), formId);
			} else if (item.type == 'gselect1') {
				gucombox = getGroupSelectCombo(item.label, item.formName,
						targetNameItem.formName, targetTypeItem.formName,
						o.root, (o.allowBlank == null || o.allowBlank), formId);
			} else if (item.type == 'uselect1') {
				gucombox = getUserChkCombo(item.label, item.formName,
						targetNameItem.formName, targetTypeItem.formName,
						o.root, (o.allowBlank == null || o.allowBlank), formId);
			} else if (item.type == 'roleselect') {
				gucombox = getRoleSingleSelectCombo(item.label, item.formName,
						targetNameItem.formName, targetTypeItem.formName,
						targetNameItem.defaultValue,
						(o.allowBlank == null || o.allowBlank), formId);
			} else if (item.type == 'roleselect1') {
				gucombox = getRoleSelectCombo(item.label, item.formName,
						targetNameItem.formName, targetTypeItem.formName,
						targetNameItem.defaultValue,
						(o.allowBlank == null || o.allowBlank), formId);
			}

			gucombox.anchor = '90%';

			if (trim(targetNameItem.defaultValue) != '') {
				gucombox.setValue(targetNameItem.defaultValue);
			}

			var guitem = gucombox;

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(guitem);
			} else {
				items.push(guitem);
			}

			hiddenItems.push(new Ext.form.Hidden({
						name : item.formName,
						value : methods.getDefaultValue(item.defaultValue,
								urlparam)
					}));

		} else if (item.type == 'dataindex') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON.decode(item.param); } catch (error) {
			 * alert('dataindex类型的视图组件<' + item.name +
			 * '>的参数格式必须为json,形如{type:\'PC\',targetName:\'dataindexName\',returnFunc:\'selectDataindex\'
			 * }'); }
			 */
			var targetNameItem;

			if (o.targetName == null) {
				alert('dataindex类型的视图组件类型的视图组件<' + item.name
						+ '>的目标组件(name)并未定义,会影响数据的完整性');
				targetNameItem = {
					formName : item.formName + '_nonametarget'
				}
			} else {
				targetNameItem = methods.findComponentItems(componentItems,
						o.targetName);

				if (targetNameItem == null) {
					alert('dataindex类型的视图组件类型的视图组件<' + item.name + '>的目标组件<id='
							+ o.targetName + '>并不存在,会影响数据的完整性');
					targetNameItem = {
						formName : item.formName + '_nonametarget'
					}
				} else {
					methods.initBeanValue(targetNameItem, options.beanData);
				}
			}

			var dditem = getDataIndexCombo(item.label, item.formName,
					targetNameItem.formName, o.targetType, o.type,
					(o.allowBlank == null || o.allowBlank), o.returnFunc,
					formId);

			if (trim(targetNameItem.defaultValue) != '') {
				dditem.setValue(targetNameItem.defaultValue);
			}

			dditem.anchor = '90%';

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(dditem);
			} else {
				items.push(dditem);
			}

			hiddenItems.push(new Ext.form.Hidden({
						name : item.formName,
						value : methods.getDefaultValue(item.defaultValue,
								urlparam)
					}));
		} else if (item.type == 'tablequery') {
			/*
			 * var o;
			 * 
			 * try { o = Ext.util.JSON .decode((item.param == '' ? '{}' :
			 * item.param)); } catch (error) { alert('tablequery类型的视图组件<' +
			 * item.name + '>的参数格式必须为json,形如{queryKey:\'test\',
			 * targetIdItem:\'\', targetItem:\'\', pointId:\'\',
			 * pointName:\'\'}'); }
			 */
			var targetItem;

			if (o.target == null) {
				alert('tablequery类型的视图组件<' + item.name + '>的目标组件并未定义,会影响数据的完整性');
				targetItem = {
					formName : item.formName + '_notarget'
				}
			} else {
				targetItem = methods.findComponentItems(componentItems,
						o.target);

				if (targetItem == null) {
					alert('tablequery类型的视图组件<' + item.name + '>的目标组件<id='
							+ o.target + '>并不存在,会影响数据的完整性');
					targetItem = {
						formName : item.formName + '_notarget'
					}
				} else {
					methods.initBeanValue(targetItem, options.beanData);
				}
			}

			if (trim(o.pointId) == '' || trim(o.pointName) == '') {
				alert('tablequery类型的视图组件<' + item.name
						+ '>的返回值pointId和pointName定义不完整,会影响返回值');
			}

			var tqitem = getTableQueryCombo(item.label, o.queryKey, null,
					function(node) {
						Ext.getCmp(formId).getForm().findField(item.formName)
								.setValue(node.get(o.pointId));
						Ext.getCmp(formId).getForm()
								.findField(targetItem.formName).setValue(node
										.get(o.pointName));
					}, item.formName, targetItem.formName, o.pointId,
					o.pointName, (o.allowBlank == null || o.allowBlank), formId);

			if (trim(targetItem.defaultValue) != '') {
				tqitem.setValue(targetItem.defaultValue);
			}

			tqitem.anchor = '90%';

			if (groupObjectInList) {
				allGroupItems[allGroupItems.length - 1].items.push(tqitem);
			} else {
				items.push(tqitem);
			}

			hiddenItems.push(new Ext.form.Hidden({
						name : item.formName,
						value : methods.getDefaultValue(item.defaultValue,
								urlparam)
					}));
		}

	});

	var viewFormArray = [];

	var columnCount = (resultJson.size.columnCount != null && /(^[1-9]\d*$)/
			.test(resultJson.size.columnCount))
			? resultJson.size.columnCount
			: 1;

	if (columnCount <= 1) {
		if (items.length > 0) {
			Ext.each(items, function(item, i) {
						viewFormArray.push(item);
					});
		}
	} else {
		var columnWidth = parseInt(10 / columnCount);

		if (items.length > 0) {
			var columnPanelOther = {
				layout : 'column',
				border : false,
				items : []
			};

			for (var i = 0; i < columnCount; i++) {
				var cp = {
					columnWidth : '.' + (columnWidth * 10),
					layout : 'form',
					border : false,
					items : []
				};

				columnPanelOther.items.push(cp);
			}

			if (items.length > 0) {
				Ext.each(items, function(item, i) {
							columnPanelOther.items[i % columnCount].items
									.push(item);
						});
			}

			viewFormArray.push(columnPanelOther);
		}
	}

	if (allGroupItems != null && allGroupItems.length > 0) {
		Ext.each(allGroupItems, function(groupObj) {
			// group columnCount set
			var groupColumnCount = (groupObj.columnCount != null && /(^[1-9]\d*$)/
					.test(groupObj.columnCount)) ? groupObj.columnCount : 1;

			var groupObjItems = groupObj.items;

			if (groupColumnCount <= 1) {
				var fieldset = {
					xtype : 'fieldset',
					title : groupObj.name,
					autoHeight : true,
					width : groupObj.width,
					layout : 'form',
					collapsed : false, // initially collapse the group
					collapsible : true,
					defaults : {
						bodyStyle : 'padding:5px'
					},
					items : [groupObjItems]
				}

				viewFormArray.push(fieldset);
			} else {
				var columnPanel = {
					layout : 'column',
					border : false,
					items : []
				}

				var columnWidth = parseInt(10 / groupColumnCount);

				for (var i = 0; i < groupColumnCount; i++) {
					var cp = {
						columnWidth : '.' + (columnWidth * 10),
						layout : 'form',
						border : false,
						items : []
					};

					columnPanel.items.push(cp);
				}

				if (groupObjItems.length > 0) {
					Ext.each(groupObjItems, function(item, i) {
								columnPanel.items[i % groupColumnCount].items
										.push(item);
							});
				}

				var fieldset = {
					xtype : 'fieldset',
					title : groupObj.name,
					autoHeight : true,
					width : groupObj.width,
					layout : 'form',
					collapsed : false, // initially collapse the group
					collapsible : true,
					defaults : {
						bodyStyle : 'padding:5px'
					},
					items : [columnPanel]
				}

				viewFormArray.push(fieldset);
			}

			if (groupObj.hookList != null) {
				// find hook list
				var cilObj = findListInTableView(groupObj.hookList,
						resultJson.componentInList);

				if (cilObj != null) {
					var grid = initListInTableView(cilObj, resultJson, options);

					viewFormArray.push(grid);
				} else {
					alert('group(' + groupObj.name + ') unhook list('
							+ groupObj.hookList + ')');
				}
			}
		});
	}

	viewFormArray.push(hiddenItems);

	var componentInList = resultJson.componentInList;

	if (componentInList != null && componentInList.length > 0) {
		// list viewList
		Ext.each(componentInList, function(cilObj, i) {
					if (Ext.getCmp(cilObj.id) != null) {
						var grid = initListInTableView(cilObj, resultJson,
								options);

						viewFormArray.push(grid);
					}

				});

	};

	formObj.id = formId;
	formObj.items = viewFormArray;
	formObj.pwdItems = pwdItems;
	formObj.checkboxes = checkboxes;
	formObj.hiddenItems = hiddenItems;
	formObj.radios = radios;
	// formObj.bodyStyle = 'padding:5px;';

	var viewForm = new Ext.FormPanel(formObj);

	return viewForm;
}

function findListInTableView(listId, componentInList) {
	var resultObj = null;
	if (componentInList != null) {
		// list viewList
		Ext.each(componentInList, function(cilObj, i) {
					if (listId == cilObj.id) {
						resultObj = cilObj;
						// methods.arrayRemove(componentInList, i);
						return false;
					}

				});
	};

	return resultObj;
}

function deleteListInTableView() {

}

/**
 * 初始化视图列表
 * 
 * @param {}
 *            cilObj
 */
function initListInTableView(cilObj, resultJson, options) {
	var griditems = [];

	var sm = new Ext.grid.CheckboxSelectionModel({
				handleMouseDown : Ext.emptyFn
			});

	griditems.push(sm);

	var fieldName = [];

	var p = {};

	var cilObjitems = cilObj.items;
	// list component in viewList
	Ext.each(cilObjitems, function(cilObjItem, j) {
				// push fieldsName
				fieldName.push({
							'name' : cilObjItem.name
						});

				if (cilObjItem.param.showInGrid == null
						|| cilObjItem.param.showInGrid == true) {
					var item = {
						header : cilObjItem.label,
						dataIndex : cilObjItem.name,
						sortable : true
					}

					if (cilObjItem.param.needRender != null
							&& cilObjItem.param.needRender == true) {
						item.renderer = function(v, c, r, row, col, s) {
							var resultObj;
							try {
								resultObj = eval(renderName
										+ "(v, c, r, row, col, s, " + cilObj.id
										+ ")");
							} catch (e) {
								return v
							}
							return resultObj;
						}
					} else if (cilObjItem.type == 'date') {
						item.renderer = function(v, c, r, row, col, s) {
							return formatDate(v);
						}
					} else if (cilObjItem.type == 'time') {
						item.renderer = function(v, c, r, row, col, s) {
							return formatTime(v);
						}
					}

					griditems.push(item);
				}
			});

	var item = {
		header : '&nbsp;',
		dataIndex : 'SP',
		sortable : false,
		renderer : function(v, c, r, row, col, s) {
			var resultObj;
			try {
				resultObj = eval(renderName + "(v, c, r, row, col, s, "
						+ cilObj.id + ")");
			} catch (e) {
				return '<a href="javascript:void(0);" onclick="javascript:editDataInDialog(\''
						+ row + '\', \'' + cilObj.id + '\');">编辑</a>'
			}
			return resultObj;
		}
	}

	griditems.push(item);

	fieldName.push({
				'name' : 'SP'
			});

	var tbar = new Ext.Toolbar({
				autoWidth : true,
				autoShow : true,
				items : [{
							iconCls : 'add',
							text : ' 增 加 ',
							handler : function() {
								newDataInDialog(grid);
							}
						}, {
							iconCls : 'delete',
							text : ' 删 除 ',
							disabled : true,
							handler : function() {
								removeDataFromGrid(grid, cilObj);
							}
						}]
			});

	// render handle
	fieldName.push({
				'name' : 'SP'
			});

	var cm = new Ext.grid.ColumnModel(griditems);

	var ds = new Ext.data.JsonStore({
				fields : fieldName
			});

	if (options.beanData != null && options.beanData[cilObj.id] != null) {
		Ext.each(options.beanData[cilObj.id], function(data) {
					var Plant = ds.recordType;

					var p = new Plant(data);

					ds.insert(ds.getCount(), p);
				});
	}

	var grid = new Ext.grid.GridPanel({
				id : cilObj.id,
				cm : cm,
				ds : ds,
				sm : sm,
				stateful : true,
				stripeRows : true,
				viewConfig : {
					forceFit : true
				},
				style : 'margin-bottom:5px',
				title : (cilObj.param.title != null ? cilObj.param.title : ''),
				width : cilObj.param.width,
				height : cilObj.param.height,
				border : false,
				frame : false,
				// config options for stateful behavior
				stateful : true,
				stateId : 'grid',
				loadMask : {
					msg : "数据加载中，请稍等..."
				},
				tbar : tbar,
				cilObj : cilObj
			});

	grid.on('click', function(e) {
				if (grid.getSelectionModel().hasSelection()) {
					tbar.items.itemAt(1).enable();
				} else {
					tbar.items.itemAt(1).disable()
				}
			});

	return grid;
}

/**
 * 
 * @param {}
 *            cilObj
 */
function newDataInDialog(grid) {
	var cilObj = grid.cilObj;

	var form = getTableViewForm({
				id : cilObj.id,
				component : cilObj.items,
				size : {
					columnCount : cilObj.param.columnCount
				}
			}, {

			}, {
				bodyStyle : 'padding:5px;',
				autoScroll : true,
				border : false,
				frame : false
			});

	var win = new Ext.Window({
				renderTo : Ext.getBody(),
				layout : 'fit',
				width : (cilObj.param.dialogWidth == null
						? 600
						: cilObj.param.dialogWidth),
				height : (cilObj.param.dialogHeight == null
						? 400
						: cilObj.param.dialogHeight),
				title : (cilObj.param.title != null ? cilObj.param.title : ''),
				plain : true,
				modal : true,
				items : [form],
				buttons : [{
					text : '确 定',
					handler : function() {
						if (form.getForm().isValid()) {
							methods.checkboxReturn(form);
							methods.radioReturn(form);

							var Plant = grid.getStore().recordType;
							var tmp = {};
							Ext.each(cilObj.items, function(cilObjItem, j) {
										var formItem = form.getForm()
												.findField(cilObjItem.formName);
										if (formItem == null) {
											alert('在list dialog form中没有找到'
													+ cilObjItem.formName);
										} else {
											tmp[cilObjItem.name] = formItem
													.getValue();
										}
									});

							var p = new Plant(tmp);

							grid.getStore().insert(grid.getStore().getCount(),
									p);

							win.close();
						}
					}
				}, {
					text : '关 闭',
					handler : function() {
						win.close();
					}
				}]
			});

	win.show();
}

/**
 * 
 * @param {}
 *            idx
 * @param {}
 *            listId
 */
function editDataInDialog(idx, listId) {
	var record = Ext.getCmp(listId).getStore().getAt(idx);

	var grid = Ext.getCmp(listId);

	var cilObj = grid.cilObj;

	Ext.each(cilObj.items, function(cilObjItem, j) {
				cilObjItem.defaultValue = record.data[cilObjItem.name];
			});

	var form = getTableViewForm({
				id : cilObj.id,
				component : cilObj.items,
				size : {
					columnCount : cilObj.param.columnCount
				}
			}, {

			}, {
				bodyStyle : 'padding:5px;',
				autoScroll : true,
				border : false,
				frame : false
			});

	var win = new Ext.Window({
				renderTo : Ext.getBody(),
				layout : 'fit',
				width : (cilObj.param.dialogWidth == null
						? 600
						: cilObj.param.dialogWidth),
				height : (cilObj.param.dialogHeight == null
						? 400
						: cilObj.param.dialogHeight),
				title : (cilObj.param.title != null ? cilObj.param.title : ''),
				plain : true,
				modal : true,
				items : [form],
				buttons : [{
					text : '确 定',
					handler : function() {
						if (form.getForm().isValid()) {
							methods.checkboxReturn(form);

							var Plant = grid.getStore().recordType;
							var tmp = {};
							Ext.each(cilObj.items, function(cilObjItem, j) {
										var formItem = form.getForm()
												.findField(cilObjItem.formName);
										if (formItem == null) {
											alert('在list dialog form中没有找到'
													+ cilObjItem.formName);
										} else {
											record.set(cilObjItem.name,
													formItem.getValue());
										}
									});
							grid.stopEditing();

							win.close();
						}
					}
				}, {
					text : '关 闭',
					handler : function() {
						win.close();
					}
				}]
			});

	win.show();
}

/**
 * 
 * @param {}
 *            grid
 */
function removeDataFromGrid(grid, cilObj) {
	var selectedNodes = grid.getSelectionModel().getSelections();
	/*
	 * for (var i = 0; i < selectedNodes.length; i++) { if (i <
	 * selectedNodes.length - 1) { ids += selectedNodes[i].data.id + ","; } else {
	 * ids += selectedNodes[i].data.id; } }
	 */

	var delRealData = false;
	Ext.each(selectedNodes, function(node, i) {
				if (trim(node.data[cilObj.forTablePrimaryColumnName]) != '') {
					delRealData = true;
					return false;
				}
			});

	if (delRealData) {
		deleteTable(cilObj.forTable, 'multi', 'counterName', function(params) {
			var counterName = '';
			Ext.each(selectedNodes, function(node, i) {
				if (trim(node.data[cilObj.forTablePrimaryColumnName]) != '') {
					counterName += i + ',';
					params[cilObj.forTablePrimaryColumnFormName + '_' + i] = node.data[cilObj.forTablePrimaryColumnName];
				}
			});

			params.counterName = counterName.substring(0, counterName.length
							- 1);
			return true;
		}, function() {
			Ext.each(selectedNodes, function(node, i) {
						grid.store.remove(node);
					});
		}, null);
	} else {
		Ext.each(selectedNodes, function(node, i) {
					grid.store.remove(node);
				});
	}
}

/**
 * 从grid中构造视图函数
 * 
 * @param {}
 *            queyrKey_
 * @return {Boolean}
 */
function initTableView4QueryList(gridId, options) {
	if (gridId == null) {
		alert('此构造函数是为通用查询列表设计的,需要传递通用查询唯一标识key');
		return false;
	}

	// var pwdItems = [];

	// 初始化视图
	this.newTableView = function(id, mode, pkid) {
		if (Ext.getCmp(gridId) == null) {
			alert('通用查询列表' + this.gridId + '为空,无法初始化视图');
		}

		options.gridId = gridId;

		if (options.tabcontainerid == null) {
			// it's
			options.tabcontainerid = 'consoletabs';
			// Ext.getCmp(gridId).ownerCt.id;
		}

		if (options.params == null) {
			options.params = {};
		}

		var url = context + "/temp-store/jsstuff/tableview/"
				+ tableviewStuff[id] + "/" + id + ".json";

		Ext.Ajax.request({
					url : url,
					params : {

		}			,
					method : 'POST',
					success : function(rs, request) {
						var result = rs.responseText;// 拿到结果集，此时为字符串

						var resultJson = Ext.util.JSON.decode(result);// 将字符串转换为json类型

						if (resultJson.success) {
							var displaymode = mode;

							if (displaymode == null) {
								displaymode = resultJson.displaymode == null
										? 'window'
										: resultJson.displaymode;
							}

							if (options.title == null) {
								options.title = resultJson.name;
							}

							if (displaymode == 'window') {
								createTableViewWindow(resultJson, options);
							} else if (displaymode == 'tab') {
								createTableViewTab(resultJson, options);
							}
						}
					}
				});

	}
}

var methods = {
	checkItemInGroup : function(checkIds, id) {
		if (checkIds == null) {
			return false;
		}
		var rnt = false;
		var ids = checkIds.split(",");
		for (var i = 0; i < ids.length; i++) {
			if (ids[i] == id) {
				rnt = true;
				break;
			}
		}
		return rnt;
	},
	/**
	 * 获取默认值
	 * 
	 * @param {}
	 *            vlu 默认值定义
	 * @param {}
	 *            param 参数map
	 * @return {}
	 */
	getDefaultValue : function(vlu, param) {
		var temp = vlu;

		if (vlu != null && vlu != '') {
			var f = vlu.substring(0, 1);

			// 默认值为获取参数类型
			if (f == '@' && param != null
					&& param.containsKey(vlu.substring(1))) {
				temp = param.get(vlu.substring(1));
			} else if (f == '@' && param != null
					&& !param.containsKey(vlu.substring(1))) {
				// 默认值为空
				temp = '';
			} else if (f == '#' && vlu.substring(1) != '') {
				// 默认值为自定义函数返回值类型
				try {
					temp = eval(vlu.substring(1) + '()');
				} catch (error) {
					alert(error.description);
				}
			}
		}

		return temp;
	},
	initBeanValue : function(item, beanData) {
		if (beanData != null) {
			if (beanData[item.formName] != null) {
				item.defaultValue = beanData[item.formName];
			} else if (beanData[item.name] != null) {
				item.defaultValue = beanData[item.name];
			}
		}
	},
	/**
	 * 找到指定的组件
	 * 
	 * @param {}
	 *            componentItems 组件列表
	 * @param {}
	 *            formName 组件formName
	 * @return {}
	 */
	findComponentItems : function(componentItems, formName) {
		var it;
		for (var i = 0; i < componentItems.length; i++) {
			if (componentItems[i].formName == formName) {
				it = componentItems[i];
				break;
			}
		}

		return it;
	},
	/**
	 * 密码确认
	 * 
	 * @param {}
	 *            p1
	 * @param {}
	 *            p2
	 * @return {Boolean}
	 */
	confirmPassword : function(p1, p2) {
		if (p1.getValue() != p2.getValue()) {
			p2.setValue('');
			Ext.Msg.alert('提示', p1.fieldLabel + '确认失败');
			return false;
		}

		return true;
	},
	/**
	 * 
	 * @param {}
	 *            form
	 * @return {}
	 */
	checkoutForm : function(form) {
		if (!form.getForm().isValid()) {
			return false;
		}

		var rnt = true;
		/*
		 * var items = form.items; for (var i = 0; i < items.length; i++) { if
		 * (items.itemAt(i).inputType == 'password' && items.itemAt(i).confirmTo !=
		 * null) { rnt = confirmPassword(Ext.getCmp(items.itemAt(i).confirmTo),
		 * items .itemAt(i)); if (!rnt) { break; } } }
		 */

		var items = form.pwdItems;

		if (items != null && items.length > 0) {
			Ext.each(items, function(item) {
						rnt = methods.confirmPassword(item, form.getForm()
										.findField(item.name + '_'));
						if (!rnt) {
							return false;
						}
					});
		}

		return rnt;
	},
	assemblingList : function(resultJson, params) {
		var rnt = true;

		if (resultJson.componentInList != null) {
			// list listInview
			Ext.each(resultJson.componentInList, function(listObj, i) {
				var grid = Ext.getCmp(listObj.id);
				if (listObj.param.allowBlank != null
						&& listObj.param.allowBlank == false
						&& grid.getStore().getCount() < 1) {

					Ext.Msg.alert('提示', (listObj.param.title == null
									? listObj.id
									: listObj.param.title)
									+ '不能为空');
					rnt = false;
					return false;
				}

				var tableName = listObj.forTable;
				var counterName = listObj.counterName;

				var counterString = '';
				// list storeInlist
				grid.store.each(function(record, j) {
					counterString += j + ',';
					// list itemInRecord
					Ext.each(listObj.items, function(item) {
						params[item.formName + '_' + j] = record.data[item.name];
					});
				});

				params[counterName] = counterString;
			});
		}

		return rnt;
	},
	checkboxReturn : function(form) {
		var items = form.checkboxes;
		if (items != null && items.length > 0) {

			for (var i = 0; i < items.length; i++) {
				var checkboxObj = items[i];

				var ids = '';
				var names = '';

				var checkboxitems = checkboxObj.checkboxGroup.items;
				if (checkboxitems != null && checkboxitems.length > 0) {
					for (var i = 0; i < checkboxitems.length; i++) {
						if (checkboxitems.itemAt(i).checked) {

							ids += checkboxitems.itemAt(i).inputValue + ',';
							names += checkboxitems.itemAt(i).boxLabel + ',';
						}
					}
				}

				form.getForm().findField(checkboxObj.idsItem)
						.setValue(ids.length > 0 ? ids.substring(0, ids.length
										- 1) : ids);
				if (checkboxObj.namesItem != null) {
					form.getForm().findField(checkboxObj.namesItem)
							.setValue(names.length > 0 ? names.substring(0,
									names.length - 1) : names);
				}
			}
		}
	},
	radioReturn : function(form) {
		var items = form.radios;
		if (items != null && items.length > 0) {

			for (var i = 0; i < items.length; i++) {
				var radioObj = items[i];

				var radioitems = radioObj.radioGroup.items;
				if (radioitems != null && radioitems.length > 0) {
					for (var i = 0; i < radioitems.length; i++) {
						if (radioitems.itemAt(i).checked) {
							form.getForm().findField(radioObj.idsItem)
									.setValue(radioitems.itemAt(i).inputValue);
							if (radioObj.namesItem != null) {
								form
										.getForm()
										.findField(radioObj.namesItem)
										.setValue(radioitems.itemAt(i).boxLabel);
							}

							break;
						}
					}
				}
			}
		}
	},
	submitViewForm : function(resultJson, form, aftercall) {
		var tableNames = resultJson.primaryTable;
		var counterNames = '-1';
		var handleTypes = 'single';

		if (resultJson.componentInList != null) {
			Ext.each(resultJson.componentInList, function(n, i) {
				tableNames += ","
						+ resultJson.foreignTables[n.forTable].foreignTable;
				counterNames += ","
						+ resultJson.foreignTables[n.forTable].foreignTableCounterName;
				handleTypes += ","
						+ resultJson.foreignTables[n.forTable].foreignTableHandleType;
			});
		}

		remixTable4Ext(tableNames, handleTypes, counterNames, function(params) {
					if (form.getForm().isValid()) {
						methods.checkboxReturn(form);

						return methods.assemblingList(resultJson, params);
					}
					return false;
				}, aftercall, form, null, resultJson.listener);
	},
	arrayRemove : function(array, dx) {
		if (array == null) {
			return false;
		}
		if (isNaN(dx) || dx > array.length) {
			return false;
		}
		for (var i = 0, n = 0; i < array.length; i++) {
			if (array[i] != array[dx]) {
				array[n++] = array[i]
			}
		}
		array.length -= 1
	},
	RMBMoney : function(money) {
		var value = trim(money);

		if (value == '') {
			return '';
		}

		var numberDelim = ',';
		var delimLength = 3;
		var prefixChar = '¥';

		if (!isNaN(money)) {
			var vSplit = value.split('.');

			var cents = (vSplit[1]) ? '.' + vSplit[1] : '';
			if (true && cents == '')
				cents = '.00';

			if (numberDelim && delimLength) {
				var numbers = vSplit[0].split('');
				var sNumbers = [];
				var c = 0;
				while (numbers.length > 0) {
					c++;
					if (c > delimLength)
						c = 1;
					sNumbers.unshift(numbers.pop());
					if (c == delimLength && numbers.length > 0)
						sNumbers.unshift(numberDelim);
				}
				value = sNumbers.join('') + cents;
			} else {
				value = vSplit[0] + cents;
			}
			if (prefixChar)
				value = prefixChar + String(value);

			return value;
		} else {
			alert('请输入数字');
			return '';
		}

		return "￥" + v;
	}
}
